package cn.jsc.org.controller;

import cn.jsc.basic.exception.BusinessException;
import cn.jsc.basic.util.AjaxResult;
import cn.jsc.basic.util.PageList;
import cn.jsc.org.domain.Shop;
import cn.jsc.org.query.ShopQuery;
import cn.jsc.org.service.IShopServiceI;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController//@ResponseBody+@Controller
@RequestMapping("/shop")
@Api(tags = "店铺接口",description = "店铺接口详细描述")

public class ShopController {

    @Autowired
    private IShopServiceI shopService;

    //删除  根据id删除  localhost:8080/dept/1
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable Long id){
        try {
            shopService.delete(id);
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createError();
        }
    }

    @PatchMapping//批量删除操作
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            shopService.patchDelete(ids);
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createError();
        }
    }

    //查询一个对象 根据id  localhost:8080/dept/1
    @GetMapping("/{id}")
    public Shop queryById(@PathVariable Long id){
        return shopService.getById(id);
    }

    /**
     * /新增或修改  根据id来区分
     *  @RequestBody :前台请求体中的json数据会帮你转成对象
     *  localhost:8080/dept
     */
    @ApiOperation(value = "店铺添加或修改",notes = "如果有id就是修改否则就是添加")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if(shop.getId()==null){
                shopService.add(shop);
            }else{
                shopService.update(shop);
            }
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createError();
        }
    }

    //查询所有的对象 localhost:8080/dept
    @GetMapping
    public List<Shop> queryAll(){
        return shopService.getAll();
    }

    //查询分页数据 localhost:8080/dept
    @PostMapping
    public PageList<Shop> queryData(@RequestBody  ShopQuery query){
        return shopService.queryData(query);
    }


    /**
     * 店铺入驻
     * @param shop
     * @return
     */
    @PostMapping
    @RequestMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("入驻失败"+e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统异常"+e.getMessage());
        }
    }

}
